package com.wanghlme.day25.sax;

import com.wanghlme.day25.dom.DomParse;
import com.wanghlme.day25.pojo.Student;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class SaxParse {
    public List<Student> parseXml() {
        List<Student> list = new ArrayList<>();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            SAXParser parser = factory.newSAXParser();
            InputStream inStream = DomParse.class.getClassLoader().getResourceAsStream("data.xml");
            SaxHandler handler = new SaxHandler();
            parser.parse(inStream, handler);//一边读文档一边进行解析
            list = handler.getResults();
            list.forEach(System.out::println);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }

        return list;
    }
}
